package org.bpms.controller.admin;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.bpms.model.admin.Suser;
import org.bpms.plugins.bjui.BjuiRender;
import org.bpms.service.admin.LogService;
import org.bpms.service.admin.UserService;

import com.jfinal.aop.Enhancer;
import com.jfinal.core.Controller;

/**
 * 后台用户管理控制器
 * @author hupei
 *
 */
public class UserController extends Controller{
	private UserService userService = Enhancer.enhance(UserService.class);
	private LogService  logService = Enhancer.enhance(LogService.class);
	/**
	 * 用户列表
	 */
	public void index(){
		Map<String,Object> pagemap = new HashMap<String, Object>();
		String tabid = getPara("tabid");
		Integer pageNumber = getParaToInt("pageCurrent", 1);//要访问第几页，默认1
		Integer pageSize = getParaToInt("pageSize", 15);//每页多少条数据，默认15
		String orderField = getPara("orderField", "id");//对什么字段排序，默认空字符串
		String orderDirection = getPara("orderDirection", "asc");//升序还是降序，默认升序
		String code = getPara("code", null);//工号
		String realname = getPara("realname", null);//姓名
		userService.findAll(code, realname, pageNumber, pageSize, orderField, 
				orderDirection, pagemap);
		setAttr("page",pagemap);
		setAttr("code", code==null?"":code);
		setAttr("realname", realname==null?"":realname);
		setAttr("tabid", tabid);
		render("/admin/user/user.html");
	}
	
	/**
	 * 编辑页面
	 */
	public void view(){
		String id = getPara("id" , "0");
		String tabid = getPara("tabid");
		Suser user = userService.findById(id);
		if (null != user)
			setAttr("user", user);
		else
			setAttr("user", new Suser());
		setAttr("tabid", tabid);
		render("/admin/user/info.html");
	}
	
	/**
	 * 选择人员
	 */
	public void selectuser(){
		Map<String,Object> pagemap = new HashMap<String, Object>();
		String ids = getPara("ids" , "");
		String names = getPara("names" , "");
		String type =getPara("type" , "");
		Integer pageNumber = getParaToInt("pageCurrent", 1);//要访问第几页，默认1
		Integer pageSize = getParaToInt("pageSize", 15);//每页多少条数据，默认15
		String orderField = getPara("orderField", "id");//对什么字段排序，默认空字符串
		String orderDirection = getPara("orderDirection", "asc");//升序还是降序，默认升序
		String code = getPara("code", null);//工号
		String realname = getPara("realname", null);//姓名
		userService.findAll(code, realname, pageNumber, pageSize, orderField, 
				orderDirection, pagemap);
		setAttr("page",pagemap);
		setAttr("code", code==null?"":code);
		setAttr("ids", ids);
		setAttr("names", names);
		setAttr("realname", realname==null?"":realname);
		if (type.equals("one")){//选择一个用户
			render("/admin/selectuser.html");
		}else{//选择多个用户
			render("/admin/selectusers.html");
		}		
	}
	
	/**
	 * 得到属于角色的用户信息
	 */
	public void userFromRole(){
		String roleId = getPara("roleid");
		List<Suser> susers = userService.userFromRole(roleId);
		setAttr("roleid", roleId);
		setAttr("susers", susers);
		render("/admin/role/role_getuser.html");
	}
	
	/**
	 * 得到属于部门的用户信息
	 */
	public void userFromDept(){
		String deptId = getPara("deptid");
		List<Suser> susers = userService.userFromDept(deptId);
		setAttr("deptid", deptId);
		setAttr("susers", susers);
		render("/admin/dept/dept_getuser.html");
	}
	
	/**
	 * 得到属于档案的用户信息
	 */
	public void userFromType(){
		String typeId = getPara("typeId");
		List<Suser> susers = userService.userFromType(typeId);
		setAttr("typeid", typeId);
		setAttr("susers", susers);
		render("/admin/type/type_getuser.html");
	}
	
	/**
	 * 保存
	 */
	public void save(){
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String  leader = getPara("leader" , null);
		String tabid = getPara("tabid");
		String  sdate  = getPara("entrytime" , "");
		Suser user = getModel(Suser.class);
		if (null == user.getStr("id") && null != userService.findByUsername(user.getStr("username"))){
			render(BjuiRender.error("账号已存在" , false));
		}else{
			if (!sdate.equals("")){
				user.set("entrytime", Timestamp.valueOf(sdate+" 00:00:00"));
			}
			user.set("leader", leader);
			if (null == user.getStr("id")){
				user.set("password", "888888");
			}
			userService.saveOrUpdate(user);
			Suser temp = getSessionAttr("user");
			logService.create(temp.getStr("username"), "保存用户:"+user.getStr("realname"));
			render(BjuiRender.success("保存成功！" , true).tabid(tabid));			
		}
	}
	
	/**
	 * 去修改密码
	 */
	public void tochangpwd(){
		render("/admin/changepwd.html");
	}
	
	/**
	 * 修改密码
	 */
	public void changpwd(){
		String id = getPara("id");
		String pass = getPara("newpassword");
		Suser user = new Suser();
		userService.saveOrUpdate(user.set("id", id).set("password", pass));
		user = userService.findById(id);
		setSessionAttr("user", user);
		render(BjuiRender.success("修改成功！" , true));
	}
	
	/**
	 * 删除用户
	 */
	public void remove(){
		String userId = getPara("userid" , "");
		Suser user = userService.findById(userId);
		Suser temp = getSessionAttr("user");
		logService.create(temp.getStr("username"), "删除用户:"+user.getStr("realname"));
		userService.delUser(userId);
		render(BjuiRender.success("删除成功！" , false));
	}
	
	public static void main(String [] args){
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = new Date();
		System.out.println(df.format(date));
	}
}
